import 'package:environmental_les_app/common/style/font_style.dart';
import 'package:environmental_les_app/common/widgets/clear_text.dart';
import 'package:environmental_les_app/common/widgets/dotted_line.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';

import '../style/colours.dart';

/// 卡片Container
baseCardContainer({
  required List<Widget> content,
  double minHeight = 0,
  void Function()? onTap,
  EdgeInsetsGeometry? padding = const EdgeInsets.all(12.0),
  EdgeInsetsGeometry? margin =
      const EdgeInsets.only(top: 10.0, left: 10.0, right: 10.0),
  CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.start,
  double? width,
  double borderRadius = 6.0,
}) {
  return InkWell(
    onTap: onTap,
    child: Container(
      margin: margin,
      padding: padding,
      width: width,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(borderRadius),
      ),
      constraints: BoxConstraints(minHeight: minHeight),
      child: Column(
        crossAxisAlignment: crossAxisAlignment,
        children: content,
      ),
    ),
  );
}

// 带边框容器
borderContainer({
  required String test,
  double borderWidth = 1.0,
  double symmetricPadding = 6.0,
  double verticalPadding = 2.0,
  double fontSize = 14.0,
  FontWeight fontWeight = FontWeight.w500,
  Color textColor = Colours.text254,
  Color borderColor = Colours.lineB7C0D7,
  Color backgroundColor = Colours.bgF4F7FE,
  double borderRadius = 4.0,
}) {
  return Container(
    padding: EdgeInsets.symmetric(
        horizontal: symmetricPadding, vertical: verticalPadding),
    decoration: BoxDecoration(
      color: backgroundColor,
      border: Border.all(color: borderColor, width: borderWidth),
      borderRadius: BorderRadius.circular(borderRadius),
    ),
    child: Text(test,
        style: TextStyle(
          fontSize: fontSize,
          fontWeight: fontWeight,
          color: textColor,
        )),
  );
}

// 带渐变背景颜色的Container
backgroundContainer({
  required Widget child,
  Color color = Colours.bgF4F7FD,
  double borderRadius = 8.0,
  EdgeInsetsGeometry? padding,
}) {
  return Container(
    decoration: BoxDecoration(
      gradient: LinearGradient(
        colors: [color.withOpacity(1.0), color.withOpacity(0.1)],
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
      ),
      borderRadius: BorderRadius.circular(borderRadius),
    ),
    padding: padding,
    child: child,
  );
}

// 染色圆环
circularRing({
  double size = 10.0,
  Color color = Colors.red,
  double borderWidth = 2.0,
}) {
  return Container(
    width: size, // 圆环的宽度
    height: size, // 圆环的高度
    decoration: BoxDecoration(
      shape: BoxShape.circle, // 设置形状为圆形
      border: Border.all(
        color: color, // 边框颜色
        width: borderWidth, // 边框（圆环）的粗细
      ),
    ),
  );
}

/// 标题
itemRowTwoStr({
  String? title,
  Widget? rightWidget,
  IconData? icon,
  double paddingTop = 10.0,
  double paddingBottom = 8.0,
}) {
  return Padding(
    padding: EdgeInsets.only(bottom: paddingBottom, top: paddingTop),
    child: Row(
      children: [
        Expanded(
          child: Row(
            children: [
              Icon(icon ?? Icons.label, color: Colours.text09C, size: 20.0),
              hGap11,
              Text(
                title ?? "",
                style: FontStyles.body,
              ),
              hGap10,
              rightWidget ?? const SizedBox(),
            ],
          ),
        ),
      ],
    ),
  );
}

statusTag({
  Color textColor = const Color(0xFFF59E0B),
  Color backgroundColor = const Color(0xFFFEF6E6),
  String statusText = "",
}) {
  return Transform(
    // 核心代码：应用变换
    // Matrix4.skewX() 接受一个弧度值来进行水平倾斜
    // 负值会使顶部向右倾斜，正值会使顶部向左倾斜
    transform: Matrix4.skewX(-0.25),
    child: Container(
      padding: const EdgeInsets.symmetric(horizontal: 6.0, vertical: 3.0),
      decoration: BoxDecoration(
        // 匹配图片中的背景色
        color: backgroundColor,
        // 设置圆角
        borderRadius: BorderRadius.circular(8.0),
      ),
      child: Text(
        statusText,
        style: TextStyle(
          // 匹配图片中的文字颜色
          color: textColor,
          fontWeight: FontWeight.bold,
          fontSize: 14,
        ),
      ),
    ),
  );
}

// 分割线
dividerLine() {
  return Container(
    height: 1.0,
    color: Colours.lineE5E6EB,
  );
}

// 虚线
dottedLine() {
  return const DottedLineWidget(
    dashWidth: 2.0,
    dashHeight: 1.0,
    dashSpace: 2.0,
    color: Colours.textC5C7D3,
    direction: Axis.horizontal, // 或者 Axis.horizontal
  );
}

/// 操作的Item信息
operateRowItem({
  String? title, //  标题
  String? content, // 内容
  GestureTapCallback? onTap, // 点击事件
  bool showLine = true, // 展示底部横线
  bool showArrow = false, // 展示右侧箭头
  double titleSize = 15.0, // 标题文字大小
  double contentSize = 15.0, // 内容文字大小
  Color contentColor = Colours.text254, // 内容文字颜色
  Widget? rightWidget, // 右侧widget
  double minHeight = 48.0, // 最小高度
  bool isRequired = false, // 是否必填
  String hintText = '', // 占位文字
  Color hintColor = Colours.textC9CDD4, // 占位文字颜色
  double hintFontSize = 14.0, // 占位文字大小
  FontWeight hintFontWeight = FontWeight.w400, // 占位文字粗细
  CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
  FontWeight? contentFontWeight,
  double paddingRight = 12.0,
}) {
  return InkWell(
    onTap: onTap,
    child: Container(
      padding: EdgeInsets.only(right: paddingRight),
      constraints: BoxConstraints(minHeight: minHeight),
      decoration: BoxDecoration(
        border: Border(
            bottom: BorderSide(
          color: showLine ? Colours.lineE5E6EB : Colors.transparent,
        )),
      ),
      child: Row(
        crossAxisAlignment: crossAxisAlignment,
        children: [
          if (isRequired)
            const Text(
              "*",
              style: TextStyle(
                  color: Colors.red,
                  fontSize: 16.0,
                  fontWeight: FontWeight.bold),
            ),
          Text(
            title ?? "",
            style: TextStyle(fontSize: titleSize, color: Colours.text969),
          ),
          hGap5,
          content == null || content.isEmpty
              ? Expanded(
                  child: Align(
                    alignment: Alignment.centerRight,
                    child: Text(
                      hintText,
                      style: TextStyle(
                          fontSize: hintFontSize,
                          color: hintColor,
                          fontWeight: hintFontWeight),
                    ),
                  ),
                )
              : Expanded(
                  child: Align(
                    alignment: Alignment.centerRight,
                    child: Text(
                      content,
                      style: TextStyle(
                        fontSize: contentSize,
                        color: contentColor,
                        fontWeight: contentFontWeight ?? FontWeight.w500,
                      ),
                    ),
                  ),
                ),
          showArrow
              ? const Icon(Icons.arrow_right, color: Colors.black)
              : rightWidget ?? const SizedBox(),
        ],
      ),
    ),
  );
}

// 展示的rowItem
Widget displayRowItem({
  String? title,
  String? content,
  Color contentColor = Colours.text254,
  double contentFontSize = 15.0,
  String? unit,
  Widget? rightWidget, // 右侧widget
  FontWeight? contentFontWeight,
}) {
  return operateRowItem(
      title: title,
      content: content,
      contentColor: contentColor,
      contentSize: contentFontSize,
      contentFontWeight: contentFontWeight,
      showLine: false,
      minHeight: 35.0,
      rightWidget: Padding(
        padding: const EdgeInsets.only(left: 3.0),
        child: rightWidget ??
            Text(
              unit ?? "",
              style: const TextStyle(
                fontSize: 14.0,
                color: Colours.text254,
                fontWeight: FontWeight.bold,
                height: 1,
              ),
            ),
      ));
}

/// 自定义Radio效果
customRadio({
  required String title,
  required int value,
  required int groupValue,
  required ValueChanged<int?> onChanged,
}) {
  return InkWell(
    onTap: () => onChanged(value),
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        value == groupValue
            ? const Icon(Icons.check_circle, size: 20, color: Colours.appMain)
            : Container(
                width: 17,
                height: 17,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  border: Border.all(color: Colours.textC9CDD4, width: 1),
                ),
              ),
        hGap5,
        Text(title),
      ],
    ),
  );
}

/// 带背景item信息
rowBgItem({
  String? title, //  标题
  String? content, // 内容
  String? unit, // 单位
  bool enableEdit = true, // 是否可编辑
  String hintText = "请输入",
  TextEditingController? controller, //控制器
  FocusNode? focusNode,
  int? maxLength,
  double? maxSum,
  TextAlign inputAlign = TextAlign.end,
  double gap = 20.0,
}) {
  return Container(
    padding: const EdgeInsets.only(right: 12.0, left: 15.0),
    constraints: const BoxConstraints(minHeight: 48.0),
    decoration: BoxDecoration(
      color: Colours.bgColor,
      borderRadius: BorderRadius.circular(6.0),
    ),
    child: Row(
      children: [
        Text(
          title ?? "",
          style: const TextStyle(fontSize: 15.0, color: Colours.text969),
        ),
        SizedBox(width: gap),
        Expanded(
          child: ClearText(
            controller: controller,
            focusNode: focusNode,
            hintText: hintText,
            maxLines: 1,
            maxLength: maxLength,
            fontWeight: FontWeight.bold,
            hintFontWeight: FontWeight.normal,
            hintColor: Colours.color989FA8,
            inputColor: enableEdit ? Colours.text254 : Colours.color989FA8,
            enabled: enableEdit,
            inputAlign: inputAlign,
            maxSum: maxSum,
            keyboardType: const TextInputType.numberWithOptions(decimal: true),
          ),
        ),
        Text(
          unit ?? "",
          style: const TextStyle(fontSize: 15.0, color: Colours.text969),
        ),
      ],
    ),
  );
}

/// 任务、报警、事件 详情页通用组件==================================================
Widget buildAddressInfoRow(
  String label,
  String value, {
  Color labelColor = Colours.text09C,
  Color valueColor = Colours.text969,
  double labelFontSize = 14,
  double valueFontSize = 14,
  FontWeight labelFontWeight = FontWeight.normal,
  FontWeight valueFontWeight = FontWeight.w500,
}) {
  return Row(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(
        label,
        style: TextStyle(
            color: labelColor,
            fontSize: labelFontSize,
            fontWeight: labelFontWeight),
      ),
      hGap8,
      Expanded(
        child: Text(
          value,
          style: TextStyle(
              color: valueColor,
              fontSize: valueFontSize,
              fontWeight: valueFontWeight),
        ),
      ),
    ],
  );
}

Widget buildInfoRow(
  String label,
  String value, {
  Color labelColor = Colours.text969,
  Color valueColor = Colours.textC4C,
  double labelFontSize = 15,
  double valueFontSize = 15,
  FontWeight labelFontWeight = FontWeight.normal,
  FontWeight valueFontWeight = FontWeight.w500,
}) {
  return Row(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(
        label,
        style: TextStyle(
            color: labelColor,
            fontSize: labelFontSize,
            fontWeight: labelFontWeight),
      ),
      const Spacer(),
      Text(
        value,
        style: TextStyle(
            color: valueColor,
            fontSize: valueFontSize,
            fontWeight: valueFontWeight),
      ),
    ],
  );
}

Widget buildExplanationInfoRow(
  String label,
  String value,
) {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Row(
        children: [
          circularRing(color: Colours.lineFF5B05),
          hGap5,
          Text(
            label,
            style: const TextStyle(
                color: Colours.text254,
                fontSize: 15,
                fontWeight: FontWeight.w500),
          ),
        ],
      ),
      vGap8,
      Text(
        value,
        style: const TextStyle(
            color: Colours.textC4C, fontSize: 15, fontWeight: FontWeight.w500),
      ),
    ],
  );
}

Widget buildProcessStep(
    String step,
    String title,
    String time,
    String department,
    String name,
    String? description,
    String nextDepartment,
    bool showLine,
    {Widget? detail}) {
  return IntrinsicHeight(
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Column(
          children: [
            Text(
              step,
              style: const TextStyle(
                  color: Colours.appMain,
                  fontSize: 18,
                  fontWeight: FontWeight.bold),
            ),
            if (showLine)
              Expanded(
                child: Container(
                  width: 1,
                  color: Colours.appMain,
                ),
              )
          ],
        ),
        hGap10,
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    title,
                    style: const TextStyle(
                        fontSize: 15,
                        fontWeight: FontWeight.w500,
                        color: Colours.textC4C),
                  ),
                  Text(
                    time,
                    style:
                        const TextStyle(fontSize: 13, color: Colours.text09C),
                  ),
                ],
              ),
              vGap10,
              Container(
                padding: const EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: Colours.bgF7F8FA,
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Container(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 8, vertical: 2),
                          decoration: BoxDecoration(
                            color: Colours.bgF4F7FE,
                            borderRadius: BorderRadius.circular(4),
                            border: Border.all(color: Colours.lineB7C0D7),
                          ),
                          child: Text(
                            department,
                            style: const TextStyle(
                                fontWeight: FontWeight.w500,
                                color: Colours.appMain,
                                fontSize: 13),
                          ),
                        ),
                        hGap8,
                        Text(
                          name,
                          style: const TextStyle(
                              color: Colours.textC4C,
                              fontSize: 14,
                              fontWeight: FontWeight.w500),
                        ),
                      ],
                    ),
                    if (description != null) ...[
                      vGap10,
                      buildAddressInfoRow("任务说明", description),
                    ],
                    if (detail != null) detail,
                    vGap10,
                    dottedLine(),
                    vGap10,
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text(
                          "下一节点主办单位",
                          style:
                              TextStyle(color: Colours.text09C, fontSize: 14),
                        ),
                        Container(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 8, vertical: 1),
                          decoration: BoxDecoration(
                            color: Colours.bgF4F7FE,
                            borderRadius: BorderRadius.circular(4),
                            border: Border.all(color: Colours.textC9CDD4),
                          ),
                          child: Text(
                            nextDepartment,
                            style: const TextStyle(
                                color: Colours.text969, fontSize: 13),
                          ),
                        ),
                      ],
                    )
                  ],
                ),
              ),
              vGap15,
            ],
          ),
        )
      ],
    ),
  );
}
